﻿using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace System.Reactive.Linq
{
	public static partial class Observable2
	{
#if !EXPERIMENTAL
		internal static IObservable<TSource> While<TSource>(Func<bool> condition, IObservable<TSource> source)
		{
			Contract.Requires(condition != null);
			Contract.Ensures(Contract.Result<IObservable<TSource>>() != null);

			Contract.Assume(source != null);

			return WhileCore(condition, source).Concat();
		}

		private static IEnumerable<IObservable<TSource>> WhileCore<TSource>(Func<bool> condition, IObservable<TSource> source)
		{
			Contract.Requires(condition != null);
			Contract.Requires(source != null);
			Contract.Ensures(Contract.Result<IEnumerable<IObservable<TSource>>>() != null);

			while (condition())
			{
				yield return source;
			}
		}
#endif
	}
}